package com.web.kdl.controller.send;

import com.web.kdl.common.result.CommonResult;
import com.web.kdl.component.ReadDeviceDataTask;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private ReadDeviceDataTask task;

    // 启动任务
    @PostMapping("/start")
    public CommonResult<String> startTask() {
        task.start();
        return CommonResult.success("任务已启动");
    }

    // 停止任务
    @PostMapping("/stop")
    public CommonResult<String> stopTask() {
        task.stop();
        return CommonResult.success("任务已停止");
    }

    // 修改间隔（单位毫秒）
    @PostMapping("/interval/{ms}/{status}")
    public CommonResult<String> updateInterval(@PathVariable long ms, @PathVariable boolean status) {
        task.updateInterval(ms,status);
        return CommonResult.success("任务间隔已修改为 " + ms + "ms");
    }

    // 查询任务状态
    @GetMapping("/status")
    public CommonResult<TaskStatus> getTaskStatus() {
        TaskStatus status = new TaskStatus();
        status.setRunning(task.isRunning());
        status.setInterval(task.getInterval());
        return CommonResult.success(status);
    }

    // 内部 DTO，用于返回任务状态
    @Data
    public static class TaskStatus {
        private boolean running;
        private long interval;

    }
}
